package com.mengze.linlee_iot_adnroid.ui.home

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.*
import android.widget.BaseAdapter
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.view.children
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.mengze.linlee_iot_adnroid.R
import com.mengze.linlee_iot_adnroid.databinding.FragmentHomeBinding


class HomeFragment : Fragment() {

    private lateinit var homeViewModel: HomeViewModel
    private var _binding: FragmentHomeBinding? = null
    private val tabTitles = listOf("白云门店", "黄埔门店", "天鹅门店", "天河门店", "越秀门店", "天河门店", "越秀门店", "天河门店", "越秀门店")
    // 数据源示例
    private val items = listOf("Item 1", "Item 2", "Item 3", "Item 4", "Item 5",
        "Item 1", "Item 2", "Item 3", "Item 4", "Item 5",
        "Item 1", "Item 2", "Item 3", "Item 4", "Item 5",
        "Item 1", "Item 2", "Item 3", "Item 4", "Item 5",
        "Item 1", "Item 2", "Item 3", "Item 4", "Item 5")


    private val binding get() = _binding!!

    fun updateStatusBar(){
        val activity = getActivity();
        // 方法1：直接设置颜色值
        activity?.window?.statusBarColor = Color.parseColor("#FFFFFFFF") // 橙色
// 方法2：使用资源颜色（推荐）
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            activity?.window?.statusBarColor = ContextCompat.getColor(activity.this, R.color.purple_200)
        }
        activity?.window.apply {
            var statusBarColor = Color.parseColor("#FFFFFFFF") // 设置颜色
        }
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        updateStatusBar();
        homeViewModel =
            ViewModelProvider(this).get(HomeViewModel::class.java)
        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root

        // 初始化Tab
        tabTitles.forEach { title ->
            val tabView = layoutInflater.inflate(
                R.layout.index__tab_item,
                binding.tabContainer,
                false
            ).apply {
                findViewById<TextView>(R.id.contentText).text = title
                setOnClickListener {
//                    selectTab(binding.tabContainer.indexOfChild(this))
                }
            }
            binding.tabContainer.addView(tabView)
        }


        binding.tabContainer.children.forEachIndexed { index, view ->
            view.setOnClickListener {
                binding.scrollView.centerItem(view)

                binding.tabContainer.children.forEachIndexed { index, view ->
                    var textView:TextView = view.findViewById( R.id.contentText )
                    textView.setTextColor(Color.parseColor("#ff333333"));
                }
                var textView:TextView = view.findViewById( R.id.contentText )
                textView.setTextColor(Color.parseColor("#ffff0000"));
            }
        }

        selectTab(0) // 默认选中第一个


        binding.gridView.apply {
            adapter = object : BaseAdapter() {
                override fun getCount() = items.size
                override fun getItem(pos: Int) = items[pos]
                override fun getItemId(pos: Int) = pos.toLong()

                override fun getView(pos: Int, convertView: View?, parent: ViewGroup): View {
                    val view = convertView ?: inflater.inflate(
                        R.layout.index__grid_item, parent, false
                    )
//                    view.findViewById<TextView>(R.id.itemText).text = items[pos]
                    return view
                }
            }
            numColumns = 2 // 关键设置：每行2列
        }

        return root
    }


    private fun selectTab(position: Int) {
        // 更新Tab状态
        (0 until binding.tabContainer.childCount).forEach { i ->
            val tab = binding.tabContainer.getChildAt(i)
            tab.isSelected = i == position
            tab.findViewById<TextView>(R.id.contentText).setTextColor(
                if (i == position) Color.RED else Color.BLACK
            )
        }

        // 滚动到可见区域
        binding.scrollView.post {
            val tabView = binding.tabContainer.getChildAt(position)
            val scrollTo = tabView.left - (binding.scrollView.width - tabView.width) / 2
            binding.scrollView.smoothScrollTo(scrollTo, 0)
        }

        // 更新内容
        updateContent(position)
    }

    private fun updateContent(position: Int) {
        println("当前显示: ${tabTitles[position]} 的内容")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}